package cn.lh.pet.service.impl;

import cn.lh.pet.domain.Pet;
import cn.lh.pet.domain.PetDetail;
import cn.lh.pet.mapper.PetDetailMapper;
import cn.lh.pet.mapper.PetMapper;
import cn.lh.pet.service.IPetService;
import cn.lh.basic.service.impl.BaseServiceImpl;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.save(petDetail);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.removeByPetId(id);
    }

    @Override
    public void onSale(Long[] ids) {
        Date onSaleTime = new Date();
        petMapper.onSale(ids, onSaleTime);
    }

    @Override
    public void offSale(Long[] ids) {
        Date offSaleTime = new Date();
        petMapper.offSale(ids, offSaleTime);
    }
}
